home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH7 / SRC / BOXES1.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-05-01  |  15.4 KB  |  493 lines

  1. VERSION 4.00
  2. Begin VB.Form BoxesForm 
  3.    Caption         =   "Boxes"
  4.    ClientHeight    =   3150
  5.    ClientLeft      =   2550
  6.    ClientTop       =   1800
  7.    ClientWidth     =   3150
  8.    Height          =   3840
  9.    Left            =   2490
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   3150
  12.    ScaleWidth      =   3150
  13.    Top             =   1170
  14.    Width           =   3270
  15.    Begin VB.HScrollBar HScrollBar 
  16.       Height          =   255
  17.       Left            =   0
  18.       TabIndex        =   2
  19.       Top             =   2880
  20.       Width           =   2895
  21.    End
  22.    Begin VB.VScrollBar VScrollBar 
  23.       Height          =   2895
  24.       Left            =   2880
  25.       TabIndex        =   1
  26.       Top             =   0
  27.       Width           =   255
  28.    End
  29.    Begin VB.PictureBox viewport 
  30.       Height          =   2880
  31.       Left            =   0
  32.       ScaleHeight     =   2820
  33.       ScaleWidth      =   2820
  34.       TabIndex        =   0
  35.       Top             =   0
  36.       Width           =   2880
  37.    End
  38.    Begin VB.Menu mnuFile 
  39.       Caption         =   "&File"
  40.       Begin VB.Menu mnuFileExit 
  41.          Caption         =   "E&xit"
  42.       End
  43.    End
  44.    Begin VB.Menu mnuScale 
  45.       Caption         =   "&Scale"
  46.       Begin VB.Menu mnuScaleZoom 
  47.          Caption         =   "&Zoom"
  48.          Shortcut        =   ^Z
  49.       End
  50.       Begin VB.Menu mnuScaleMag 
  51.          Caption         =   "Full  Scale"
  52.          Index           =   1
  53.          Shortcut        =   ^F
  54.       End
  55.       Begin VB.Menu mnuScaleMag 
  56.          Caption         =   "Magnify 1/2"
  57.          Index           =   20
  58.          Shortcut        =   ^{F2}
  59.       End
  60.       Begin VB.Menu mnuScaleMag 
  61.          Caption         =   "Magnify 1/4"
  62.          Index           =   40
  63.          Shortcut        =   ^{F4}
  64.       End
  65.    End
  66. Attribute VB_Name = "BoxesForm"
  67. Attribute VB_Creatable = False
  68. Attribute VB_Exposed = False
  69. Option Explicit
  70. Dim ThePicture As ObjPicture
  71. ' Global max and min world coordinates
  72. ' (including margins).
  73. Dim DataXmin As Single
  74. Dim DataXmax As Single
  75. Dim DataYmin As Single
  76. Dim DataYmax As Single
  77. ' Set the min and max allowed width and height.
  78. Dim DataMinWid As Single
  79. Dim DataMinHgt As Single
  80. Dim DataMaxWid As Single
  81. Dim DataMaxHgt As Single
  82. ' The aspect ratio of the viewport.
  83. Dim VAspect As Single
  84. ' Current world window bounds.
  85. Dim Wxmin As Single
  86. Dim Wxmax As Single
  87. Dim Wymin As Single
  88. Dim Wymax As Single
  89. ' Prevent change events when we are adjusting the
  90. ' scroll bars.
  91. Dim IgnoreSbarChange As Boolean
  92. ' Variables used for zooming.
  93. Dim DrawingMode As Integer
  94. Const MODE_NONE = 0
  95. Const MODE_START_ZOOM = 1
  96. Const MODE_ZOOMING = 2
  97. Dim StartX As Single
  98. Dim StartY As Single
  99. Dim LastX As Single
  100. Dim LastY As Single
  101. Dim OldMode As Integer
  102. ' The object that is highlighted.
  103. Dim highlighted As Object
  104. ' ************************************************
  105. ' End a zoom operation early. This happens if the
  106. ' user starts a zoom and the selects another menu
  107. ' item instead of doing the zoom.
  108. ' ************************************************
  109. Sub StopZoom()
  110.     If DrawingMode <> MODE_START_ZOOM Then Exit Sub
  111.     DrawingMode = MODE_NONE
  112.     Viewport.DrawMode = OldMode
  113.     Viewport.MousePointer = vbDefault
  114. End Sub
  115. ' ************************************************
  116. ' Change the level of magnification.
  117. ' ************************************************
  118. Sub SetScaleFactor(fact As Single)
  119. Dim wid As Single
  120. Dim hgt As Single
  121. Dim mid As Single
  122.     fact = 1 / fact
  123.     ' Compute the new world window size.
  124.     wid = fact * (Wxmax - Wxmin)
  125.     hgt = fact * (Wymax - Wymin)
  126.     ' Center the new world window over the old.
  127.     mid = (Wxmax + Wxmin) / 2
  128.     Wxmin = mid - wid / 2
  129.     Wxmax = mid + wid / 2
  130.     mid = (Wymax + Wymin) / 2
  131.     Wymin = mid - hgt / 2
  132.     Wymax = mid + hgt / 2
  133.     ' Set the new world window bounds.
  134.     SetWorldWindow
  135. End Sub
  136. ' ************************************************
  137. ' Adjust the world window so it is not too big,
  138. ' too small, off to one side, or of the wrong
  139. ' aspect ratio. Then map the world window to the
  140. ' viewport and force the viewport to repaint.
  141. ' ************************************************
  142. Sub SetWorldWindow()
  143. Dim wid As Single
  144. Dim hgt As Single
  145. Dim xmid As Single
  146. Dim ymid As Single
  147. Dim aspect As Single
  148.     wid = Wxmax - Wxmin
  149.     xmid = (Wxmax + Wxmin) / 2
  150.     hgt = Wymax - Wymin
  151.     ymid = (Wymax + Wymin) / 2
  152.         
  153.     ' Make sure we're not too big or too small.
  154.     If wid > DataMaxWid Then
  155.         wid = DataMaxWid
  156.     ElseIf wid < DataMinWid Then
  157.         wid = DataMinWid
  158.     End If
  159.     If hgt > DataMaxHgt Then
  160.         hgt = DataMaxHgt
  161.     ElseIf hgt < DataMinHgt Then
  162.         hgt = DataMinHgt
  163.     End If
  164.     ' Make the aspect ratio match the
  165.     ' viewport aspect ratio.
  166.     aspect = hgt / wid
  167.     If aspect > VAspect Then
  168.         ' Too tall and thin. Make it wider.
  169.         wid = hgt / VAspect
  170.     Else
  171.         ' Too short and wide. Make it taller.
  172.         hgt = wid * VAspect
  173.     End If
  174.     ' Compute the new coordinates
  175.     Wxmin = xmid - wid / 2
  176.     Wxmax = xmid + wid / 2
  177.     Wymin = ymid - hgt / 2
  178.     Wymax = ymid + hgt / 2
  179.     ' Check that we're not off to one side.
  180.     If wid > DataMaxWid Then
  181.         ' We're wider than the picture. Center.
  182.         xmid = (DataXmax + DataXmin) / 2
  183.         Wxmin = xmid - wid / 2
  184.         Wxmax = xmid + wid / 2
  185.     Else
  186.         ' Else see if we're too far to one side.
  187.         If Wxmin < DataXmin And Wxmax < DataXmax Then
  188.             ' Adjust to the right.
  189.             Wxmax = Wxmax + DataXmin - Wxmin
  190.             Wxmin = DataXmin
  191.         End If
  192.         If Wxmax > DataXmax And Wxmin > DataXmin Then
  193.             ' Adjust to the left.
  194.             Wxmin = Wxmin + DataXmax - Wxmax
  195.             Wxmax = DataXmax
  196.         End If
  197.     End If
  198.     If hgt > DataMaxHgt Then
  199.         ' We're taller than the picture. Center.
  200.         ymid = (DataYmax + DataYmin) / 2
  201.         Wymin = ymid - hgt / 2
  202.         Wymax = ymid + hgt / 2
  203.     Else
  204.         ' See if we're too far to top or bottom.
  205.         If Wymin < DataYmin And Wymax < DataYmax Then
  206.             ' Adjust downward.
  207.             Wymax = Wymax + DataYmin - Wymin
  208.             Wymin = DataYmin
  209.         End If
  210.         If Wymax > DataYmax And Wymin > DataYmin Then
  211.             ' Adjust upward.
  212.             Wymin = Wymin + DataYmax - Wymax
  213.             Wymax = DataYmax
  214.         End If
  215.     End If
  216.     ' Map the world window to the viewport.
  217.     Viewport.Scale (Wxmin, Wymax)-(Wxmax, Wymin)
  218.     ' Force the viewport to repaint.
  219.     Viewport.Refresh
  220.         
  221.     ' Reset the scroll bars.
  222.     IgnoreSbarChange = True
  223.     HScrollBar.Visible = (wid < DataXmax - DataXmin)
  224.     VScrollBar.Visible = (hgt < DataYmax - DataYmin)
  225.     ' The values of the scroll bars will be where
  226.     ' the top/left of the world window should be.
  227.     VScrollBar.Min = 100 * (DataYmax)
  228.     VScrollBar.Max = 100 * (DataYmin + hgt)
  229.     HScrollBar.Min = 100 * (DataXmin)
  230.     HScrollBar.Max = 100 * (DataXmax - wid)
  231.     ' SmallChange moves the world window 1/10
  232.     ' of its width/height. Large change moves it
  233.     ' 9/10 of its width/height.
  234.     VScrollBar.SmallChange = 100 * (hgt / 10)
  235.     VScrollBar.LargeChange = 100 * (9 * hgt / 10)
  236.     HScrollBar.SmallChange = 100 * (wid / 10)
  237.     HScrollBar.LargeChange = 100 * (9 * wid / 10)
  238.     ' Set the current scroll bar values.
  239.     VScrollBar.Value = 100 * Wymax
  240.     HScrollBar.Value = 100 * Wxmin
  241.     IgnoreSbarChange = False
  242. End Sub
  243. ' ************************************************
  244. ' Return to the default magnification scale.
  245. ' ************************************************
  246. Sub SetScaleFull()
  247.     ' Reset the world window coordinates.
  248.     Wxmin = DataXmin
  249.     Wxmax = DataXmax
  250.     Wymin = DataYmin
  251.     Wymax = DataYmax
  252.     ' Set the new world window bounds.
  253.     SetWorldWindow
  254. End Sub
  255. Private Sub Form_Resize()
  256. Dim x As Single
  257. Dim y As Single
  258. Dim wid As Single
  259. Dim hgt As Single
  260.     MakeBoxes
  261.     ' Fit the viewport to the window.
  262.     x = Viewport.Left
  263.     y = Viewport.Top
  264.     wid = ScaleWidth - 2 * x - VScrollBar.Width
  265.     hgt = ScaleHeight - 2 * y - HScrollBar.Height
  266.     Viewport.Move x, y, wid, hgt
  267.     VAspect = hgt / wid
  268.     ' Place the scroll bars next to the viewport.
  269.     x = Viewport.Left + Viewport.Width + 10
  270.     y = Viewport.Top
  271.     wid = VScrollBar.Width
  272.     hgt = Viewport.Height
  273.     VScrollBar.Move x, y, wid, hgt
  274.     x = Viewport.Left
  275.     y = Viewport.Top + Viewport.Height + 10
  276.     wid = Viewport.Width
  277.     hgt = HScrollBar.Height
  278.     HScrollBar.Move x, y, wid, hgt
  279.     ' Start at full scale.
  280.     SetScaleFull
  281. End Sub
  282. Sub MakeBoxes()
  283. Const NUM_ROWS = 50
  284. Const NUM_COLS = 50
  285. Dim poly As ObjPolygon
  286. Dim i As Integer
  287. Dim j As Integer
  288. Dim x As Single
  289. Dim y As Single
  290. Dim wid As Single
  291. Dim hgt As Single
  292.     MousePointer = vbHourglass
  293.     DoEvents
  294.     Set ThePicture = New ObjPicture
  295.     y = 0
  296.     For i = 1 To NUM_ROWS
  297.         x = 0
  298.         For j = 1 To NUM_COLS
  299.             Set poly = New ObjPolygon
  300.             ThePicture.Objects.Add poly
  301.             poly.NumPoints = 5
  302.             poly.SetPoint 1, x, y
  303.             poly.SetPoint 2, x + 1, y
  304.             poly.SetPoint 3, x + 1, y + 1
  305.             poly.SetPoint 4, x, y + 1
  306.             poly.SetPoint 5, x, y
  307.             x = x + 2
  308.         Next j
  309.         y = y + 2
  310.     Next i
  311.     wid = 2 * NUM_COLS + 1
  312.     hgt = 2 * NUM_ROWS + 1
  313.     DataXmin = -0.1 * wid   ' 10 % margins.
  314.     DataYmin = -0.1 * hgt
  315.     DataXmax = 1.1 * wid
  316.     DataYmax = 1.1 * hgt
  317.     DataMinWid = 10
  318.     DataMinHgt = 10
  319.     DataMaxWid = DataXmax - DataXmin
  320.     DataMaxHgt = DataYmax - DataYmin
  321.     MousePointer = vbDefault
  322. End Sub
  323. ' ************************************************
  324. ' Move the world window.
  325. ' ************************************************
  326. Private Sub HScrollBar_Change()
  327.     If IgnoreSbarChange Then Exit Sub
  328.     HScrollBarChanged
  329. End Sub
  330. ' ************************************************
  331. ' The vertical scroll bar has been moved. Adjust
  332. ' the world window.
  333. ' ************************************************
  334. Sub VScrollBarChanged()
  335. Dim hgt As Single
  336.     hgt = Wymax - Wymin
  337.     Wymax = VScrollBar.Value / 100
  338.     Wymin = Wymax - hgt
  339.     ' Remap the world window.
  340.     IgnoreSbarChange = True
  341.     SetWorldWindow
  342.     IgnoreSbarChange = False
  343. End Sub
  344. ' ************************************************
  345. ' The horizontal scroll bar has been moved. Adjust
  346. ' the world window.
  347. ' ************************************************
  348. Sub HScrollBarChanged()
  349. Dim wid As Single
  350.     wid = Wxmax - Wxmin
  351.     Wxmin = HScrollBar.Value / 100
  352.     Wxmax = Wxmin + wid
  353.     ' Remap the world window.
  354.     IgnoreSbarChange = True
  355.     SetWorldWindow
  356.     IgnoreSbarChange = False
  357. End Sub
  358. Private Sub mnuFileExit_Click()
  359.     StopZoom    ' If we're zooming, stop it.
  360.     Unload Me
  361. End Sub
  362. ' ************************************************
  363. ' Change the level of magnification.
  364. ' ************************************************
  365. Private Sub mnuScaleMag_Click(Index As Integer)
  366.     StopZoom    ' If we're zooming, stop it.
  367.     If Index = 1 Then
  368.         ' Return to full scale.
  369.         SetScaleFull
  370.     ElseIf Index < 10 Then
  371.         ' Magnify by the indicated amount.
  372.         SetScaleFactor CSng(Index)
  373.     Else
  374.         ' Zoom out by 1/(Index \ 10).
  375.         SetScaleFactor 1 / (Index \ 10)
  376.     End If
  377. End Sub
  378. ' ************************************************
  379. ' Allow the user to select an area to zoom in on.
  380. ' ************************************************
  381. Private Sub mnuScaleZoom_Click()
  382.     ' Enable zooming.
  383.     Viewport.MousePointer = vbCrosshair
  384.     DrawingMode = MODE_START_ZOOM
  385. End Sub
  386. ' ************************************************
  387. ' If we are zooming, start the rubberband box.
  388. ' ************************************************
  389. Private Sub Viewport_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
  390.     Select Case DrawingMode
  391.         Case MODE_START_ZOOM
  392.             ' Start a zooming rubberband box.
  393.             DrawingMode = MODE_ZOOMING
  394.         
  395.             OldMode = Viewport.DrawMode
  396.             Viewport.DrawMode = vbInvert
  397.             
  398.             StartX = x
  399.             StartY = y
  400.             LastX = x
  401.             LastY = y
  402.             Viewport.Line (StartX, StartY)-(LastX, LastY), , B
  403.         
  404.         Case MODE_NONE
  405.             ' Select a box.
  406.             Dim oldcolor As Long
  407.             
  408.             ' Unhighlight the previous box.
  409.             If Not highlighted Is Nothing Then
  410.                 highlighted.Draw Viewport
  411.             End If
  412.             
  413.             ' Find the selected box.
  414.             Set highlighted = ThePicture.NearestObject(x, y)
  415.             ' Highlight the selected box.
  416.             If Not highlighted Is Nothing Then
  417.                 oldcolor = Viewport.ForeColor
  418.                 Viewport.ForeColor = RGB(0, 255, 0)
  419.                 highlighted.Draw Viewport
  420.                 Viewport.ForeColor = oldcolor
  421.             End If
  422.             
  423.     End Select
  424. End Sub
  425. ' ************************************************
  426. ' If we are zooming, continue the rubberband box.
  427. ' ************************************************
  428. Private Sub Viewport_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
  429.     If DrawingMode <> MODE_ZOOMING Then Exit Sub
  430.     ' Erase the old box.
  431.     Viewport.Line (StartX, StartY)-(LastX, LastY), , B
  432.     ' Draw the new box.
  433.     LastX = x
  434.     LastY = y
  435.     Viewport.Line (StartX, StartY)-(LastX, LastY), , B
  436. End Sub
  437. ' ************************************************
  438. ' If we are zooming, finish the rubberband box.
  439. ' ************************************************
  440. Private Sub Viewport_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
  441. Dim wid As Single
  442. Dim hgt As Single
  443. Dim mid As Single
  444.     If DrawingMode <> MODE_ZOOMING Then Exit Sub
  445.     DrawingMode = MODE_NONE
  446.     ' Erase the old box.
  447.     Viewport.Line (StartX, StartY)-(LastX, LastY), , B
  448.     LastX = x
  449.     LastY = y
  450.     ' We're done drawing for this rubberband box.
  451.     Viewport.DrawMode = OldMode
  452.     Viewport.MousePointer = vbDefault
  453.     ' Set the new world window bounds.
  454.     If StartX > LastX Then
  455.         Wxmin = LastX
  456.         Wxmax = StartX
  457.     Else
  458.         Wxmin = StartX
  459.         Wxmax = LastX
  460.     End If
  461.     If StartY > LastY Then
  462.         Wymin = LastY
  463.         Wymax = StartY
  464.     Else
  465.         Wymin = StartY
  466.         Wymax = LastY
  467.     End If
  468.     ' Set the new world window bounds.
  469.     SetWorldWindow
  470. End Sub
  471. Private Sub Viewport_Paint()
  472. Dim oldcolor As Long
  473.     If ThePicture Is Nothing Then Exit Sub
  474.     MousePointer = vbHourglass
  475.     DoEvents
  476.     ThePicture.Draw Viewport
  477.     ' If a box is selected, highlight it.
  478.     If Not highlighted Is Nothing Then
  479.         oldcolor = Viewport.ForeColor
  480.         Viewport.ForeColor = RGB(0, 255, 0)
  481.         highlighted.Draw Viewport
  482.         Viewport.ForeColor = oldcolor
  483.     End If
  484.     MousePointer = vbDefault
  485. End Sub
  486. ' ************************************************
  487. ' Move the world window.
  488. ' ************************************************
  489. Private Sub VScrollBar_Change()
  490.     If IgnoreSbarChange Then Exit Sub
  491.     VScrollBarChanged
  492. End Sub
  493.